package com.jiuyun.servlet;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jiuyun.dao.ExamDao;
import com.jiuyun.dao.Exam_recordDao;
import com.jiuyun.dao.QuestionDao;
import com.jiuyun.dao.impi.ExamDaoImpl;
import com.jiuyun.dao.impi.Exam_recordDaoImpi;
import com.jiuyun.dao.impi.QuestionDaoImpi;
import com.jiuyun.entity.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/question/*")
public class QuestionServlet extends BaseServlet {
    ExamDao examDao = new ExamDaoImpl();
    QuestionDao questionDao = new QuestionDaoImpi();
    Gson gson = new Gson();
    Exam_recordDao dao=new Exam_recordDaoImpi();

    protected void findtm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("开始考试");
        String exam_id=request.getParameter("exam_id");
        int a = 0;
        Exam1 exam = examDao.find(exam_id);
        List<Question1> questionlist = questionDao.find(exam.getExam_code());
        List<Question1> tmlist = questionDao.tmfind(exam.getExam_code(), a);
        HttpSession session = request.getSession();
        session.setAttribute("tmlist", tmlist);
        session.setAttribute("exam",exam);
        session.setAttribute("tmll", questionlist);
        session.setAttribute("questionlist", questionlist);
        session.setAttribute("size", questionlist.size());
        response.sendRedirect("/Eventstar/dati.jsp");
    }

    protected void tm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String exam_id=request.getParameter("exam_id");
        Exam1 exam = examDao.find(exam_id);
        PrintWriter out = response.getWriter();
        response.setContentType("application/json");
        int a=0;
        a=a+(Integer.parseInt(request.getParameter("ym"))-1);
        List<Question1> tmlist = questionDao.tmfind(exam.getExam_code(), a);
        String t = gson.toJson(tmlist);
        String t1=gson.toJson(exam);
        out.print(t);
        out.close();
        out.flush();
    }

    protected void shan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("上一题");
        String exam_id = request.getParameter("exam_id");
        Exam1 exam = examDao.find(exam_id);
        PrintWriter out = response.getWriter();
        response.setContentType("application/json");
        int a = 0;
        a = (Integer.parseInt(request.getParameter("ym")));
        List<Question1> tmlist = questionDao.tmfind(exam.getExam_code(), a);
        String t = gson.toJson(tmlist);
        String t1=gson.toJson(exam);
        out.print(t);
        out.close();
        out.flush();
    }

    protected void tijiao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("存储题目");
        String sz =request.getParameter("sz");
        System.out.println(sz);
        List<DaAn> da =gson.fromJson(sz, new TypeToken<List<DaAn>>() {}.getType());
        System.out.println(da);
        Exam1 exam=null;
        List<Question1> list=null;
        Question1 question=null;
        int fen=0;
        int dui=0;
        int cuo=0;
        String zonda="";

        for (DaAn daan : da)
        {
            zonda+=daan.getQuestion_id()+"="+daan.getDaan()+",";
            exam=examDao.find1(daan.getExam_name());
            System.out.println(exam);
            list=questionDao.find(exam.getExam_code());
            System.out.println(list);
            question=questionDao.js(daan.getDaan(),daan.getQuestion_id(),daan.getExam_name());
            if(question==null)
            {
                cuo++;
            }else
            {
                if(daan.getDaan().equals(question.getQuestion_answer()))
                {
                    System.out.println(question.getQuestion_answer());
                    fen=fen+Integer.parseInt(question.getQuestion_fraction());
                    dui++;
                }
            }
        }
        int num=dui+cuo;
        String exam_name=exam.getExam_name();
        XS xs=new XS(fen,num,dui,cuo,exam_name);
        System.out.println("成绩:"+fen+"--"+"总题目:"+num+"--"+"答对："+dui+"--"+"答错:"+cuo+"--试卷名:"+exam.getExam_name());
        System.out.println(zonda);
        int a=dao.add(218,exam.getExam_id(),fen+"",zonda);
        System.out.println(xs);
        PrintWriter out = response.getWriter();
        response.setContentType("application/json");
        String t=gson.toJson(xs);
        out.print(t);
        out.close();
        out.flush();
    }

    protected void ajax2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("考试名称");
        String exam_id = request.getParameter("exam_id");
        Exam1 exam = examDao.find(exam_id);
        PrintWriter out = response.getWriter();
        response.setContentType("application/json");
        String t=gson.toJson(exam);
        out.print(t);
        out.close();
        out.flush();
    }

    protected void grade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("开始跳转");
        HttpSession session=request.getSession();
        int fen=Integer.parseInt(request.getParameter("fen"));
        int dui=Integer.parseInt(request.getParameter("dui"));
        int cuo=Integer.parseInt(request.getParameter("cuo"));
        String exam_name=request.getParameter("exam_name");
        int num=dui+cuo;
        session.setAttribute("fen",fen);
        session.setAttribute("num",num);
        session.setAttribute("dui",dui);
        session.setAttribute("cuo",cuo);
        session.setAttribute("exam_name",exam_name);
        response.sendRedirect("/Eventstar/grade.jsp");
    }
}

